<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
    <script src="/js/jquery-1.11.3.min.js" type="text/javascript"></script>
</head>
<body>
<form id="loginForm">
    用户名：<input id="username" type="text" name="username"><br>
    密  码：<input id="password" type="password" name="password"><br>
    <button type="button" onclick="sumbit()">提交</button>
</form>

<script>
    function sumbit(){
        var username =$("#username").val();
        var password = $("#password").val();
        if(username == null){
            alert("用户名不能为空")
        }else if(password == null){
            alert("密码不能为空")
        }else{
            $.ajax({
                async: false,//同步，待请求完毕后再执行后面的代码
                type: "POST",
                url: '/login',
                contentType: "application/x-www-form-urlencoded; charset=utf-8",
                data: $("#loginForm").serialize(),
                dataType: "json",
                success: function (data) {
                    console.log("data: ",data)
                    if(data.code==2) {
                        alert(data.msg)
                        window.location.href="/loginSuccess"
                    }else{
                        alert(data.msg)
                    }
                },
                error: function () {
                    layer.alert("数据获取失败")
                }
            })
        }
    }
</script>
</body>
</html>